<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Swoole\Coroutine as co;

class Process extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'process:run';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        //
       /* $proc1=new \swoole_process(function(\swoole_process $proc){
            $socket=$proc->exportSocket();
            echo $socket->recv();
            $socket->send("hello proc2\n");
            echo "proc1 stop\n";
        },false,1,true);
       // assert($proc1->start());
       $proc1->start();
        \swoole_process::wait(true);*/
        //assert($proc1->start());
        /*$proc = new \swoole_process(function () {
            co::sleep(0.2);
            echo "SUCCESS\n";
        }, false, 1, true);
        $proc->start();*/
        $proc1 = new \swoole_process(function (\swoole_process $proc) {
            $socket = $proc->exportSocket();
            echo $socket->recv();
            $socket->send("hello proc2\n");
            echo "proc1 stop\n";
        }, false, 1, true);
        
        assert($proc1->start());
        
        $proc2 = new \swoole_process(function (\swoole_process $proc) use ($proc1) {
            Co::sleep(0.01);
            $socket = $proc1->exportSocket();
            $socket->send("hello proc1\n");
            echo $socket->recv();
            echo "proc2 stop\n";
        }, false, 0, true);
        assert($proc2->start());
        \swoole_process::wait(true);
        \swoole_process::wait(true);
    }
  
}
